import subprocess
import socket

def run_nslookup(domain, dns_server):
    try:
        # 执行 nslookup 命令并捕获输出
        result = subprocess.run(
            ['nslookup', '-timeout=5', domain, dns_server],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True,
            timeout=10
        )
        return result.stdout
    except subprocess.TimeoutExpired:
        return "请求超时"

def analyze_nslookup_output(output, domain):
    if "Aliases" in output:
        print(f"{domain} 使用CDN")
    else:
        try:
            ip_address = socket.gethostbyname(domain)
            print(f"The IP address of {domain} is: {ip_address}")
        except socket.gaierror:
            print(f"{domain} 无法解析")

def main(domain_file, dns_server):
    with open(domain_file, 'r') as file:
        domains = file.readlines()

    for domain in domains:
        domain = domain.strip()  # 去除行尾的换行符
        if domain:  # 确保域名不为空
            output = run_nslookup(domain, dns_server)
            analyze_nslookup_output(output, domain)

if __name__ == "__main__":
    import sys
    if len(sys.argv) != 2:
        print("Usage: python test.py domain.txt")
        sys.exit(1)

    domain_file = sys.argv[1]
    dns_server = "119.29.29.29"
    main(domain_file, dns_server)

print(f"检查完毕")